package com.flying.carclub.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.api.ApiImpl;
import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.SPUtils;
import com.flying.carclub.widget.ClearEditText;
import com.flying.carclub.widget.InfoBar;
import com.flying.carclub.widget.TitleBar;

public class EditInfoActivity extends BaseActivity {
    public static String EXTRA_TYPE = "EXTRA_FROM";
    public static int TYPE_NAME = 0x001;
    public static int TYPE_NICKNAME = 0x010;
    private ClearEditText contentEt;
    private TitleBar titleBar;
    private UserInfoBean userInfoBean;
    private String key = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);
        titleBar = findViewById(R.id.title_bar);
        contentEt = findViewById(R.id.et_content);
        userInfoBean = SPUtils.getUserInfo(EditInfoActivity.this);
        int from = getIntent().getIntExtra(EXTRA_TYPE, TYPE_NAME);
        if (from == TYPE_NAME) {
            key = "realname";
            contentEt.setHint("请输入您的姓名");
            titleBar.setTitle("姓名");
            if (userInfoBean != null)
                contentEt.setText(userInfoBean.realName);
        } else if (from == TYPE_NICKNAME) {
            key = "nickname";
            contentEt.setHint("请输入昵称");
            titleBar.setTitle("昵称");
            if (userInfoBean != null)
                contentEt.setText(userInfoBean.nickname);
        }

        contentEt.setSelection(contentEt.getText().toString().trim().length());

        titleBar.setOnTitleClickListener(new TitleBar.OnTitleClickListener() {
            @Override
            public boolean onLeftClick() {
                return false;
            }

            @Override
            public void onRightClick() {
                if (TextUtils.isEmpty(contentEt.getText())){
                    showToast("输入内容不能为空");
                    return;
                }
                String username  = userInfoBean!=null ? userInfoBean.username:"";
                showLoadingDialog();
                new ApiImpl().httpUpdateUserInfo(username, key, contentEt.getText().toString().trim(), new OnResponseListener() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        UserInfoBean userInfoBean = (UserInfoBean) response;
                        if (userInfoBean != null) {
                            SPUtils.setUserInfo(EditInfoActivity.this, userInfoBean);
                        }
                        showToast("更新成功");
                        setResult(RESULT_OK);
                        EditInfoActivity.this.finish();
                    }

                    @Override
                    public void onError(String err) {
                        dismissLoadingDialog();
                        if (TextUtils.isEmpty(err)){
                            err = "网络请求失败";
                        }
                        showToast(err);
                    }
                });
            }
        });
    }
}
